/*
 * Copyright ©2021 su binglun(9085309@qq.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *---------------------------------------------------------------------
 *-                                                                   -
 *-           Website : http://phoenix.sunny360.cn/                   -
 *-                                                                   -
 *---------------------------------------------------------------------
 */
package cn.sunny360.core.common;

import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;

import java.io.PrintWriter;
import java.io.StringWriter;

public class PhResult {
    private JsonObject jsonObject;

    public static PhResult ok() {
        return new PhResult(200, "OK", null);
    }

    public static PhResult ok(String msg) {
        return new PhResult(200, msg, null);
    }

    public static PhResult ok(String msg, String data) {
        return new PhResult(200, msg, data);
    }

    public static PhResult error(int code, String msg) {
        return new PhResult(code, msg, null);
    }

    public static PhResult error(int code, Throwable t) {
        String data = "";
        String msg = "";
        if (t != null) {
            StringWriter writer = new StringWriter();
            PrintWriter printWriter = new PrintWriter(writer);
            t.printStackTrace(printWriter);
            data = writer.toString();
            msg = t.getMessage();
        } else {
            msg = "系统异常，无异常信息";
        }
        return new PhResult(code, msg, data);
    }

    public static PhResult error(int code, String msg, String data) {
        return new PhResult(code, msg, data);
    }

    public static PhResult result(int code, String msg, String data) {
        return new PhResult(code, msg, data);
    }

    private PhResult(int code, String msg, String data) {
        jsonObject = new JsonObject();
        jsonObject.put("code", code);
        jsonObject.put("msg", msg);
        jsonObject.put("data", data);
    }

    public int getCode() {
        return jsonObject.getInteger("code");
    }

    public void setCode(int code) {
        this.jsonObject.put("code", code);
    }

    public String getMsg() {
        return jsonObject.getString("msg");
    }

    public void setMsg(String msg) {
        this.jsonObject.put("msg", msg);
    }

    public String getData() {
        return jsonObject.getString("data");
    }

    public void setData(String data) {
        this.jsonObject.put("data", data);
    }

    @Override
    public String toString() {
        return jsonObject.toString();
    }

    public Buffer toBuffer() {
        return jsonObject.toBuffer();
    }
}
